<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <img data-src="https://cdn.pixabay.com/photo/2021/04/27/13/32/birds-6211541__340.png" alt="">
  <script>
    setTimeout(() => {
      console.log("加载图片")
      let img = document.getElementsByTagName("img")[0]
      loadImg(img).then(res=>{
        console.log("加载完成")
      }).catch(err=>{
        console.log(err)
      })
    }, 2000);
    function loadImg(img){
      return new Promise((resolve, reject)=>{
        img.onload = ()=>{
          console.log("onload加载完成")
          resolve()
        }
        img.onerror = (e)=>{
          reject(e)
        }
        img.src = img.dataset.src
      })
    }
  </script>
</body>
</html>